<template>
  <div class="leaveHospital-search">
    <el-form :inline="true">
      <el-form-item>
        <el-select
          v-model="conditionList.departmentId"
          placeholder="所有科室"
          clearable
          @change="departmentChange"
        >
          <el-option
            v-for="item of departmentlist"
            :key="item.id"
            :label="item.departmentName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="conditionList.doctorId"
          placeholder="所有医生"
          clearable
        >
          <el-option
            v-for="item of doctorlist"
            :key="item.doctorId"
            :label="item.doctorName"
            :value="item.doctorId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="conditionList.diabetesType"
          placeholder="糖尿病类型"
          clearable
        >
          <el-option
            v-for="item of sugarTypelist"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="conditionList.patientName"
          placeholder="姓名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="conditionList.hospitalNum"
          placeholder="住院号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="conditionList.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="conditionList.time"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item class="btn">
        <el-button type="primary" @click="getAllOutInfo">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import bus from "./bus.js";
export default {
  data() {
    return {
      conditionList: {
        time: "", //选择日期
        departmentId: "", //科室ID
        diabetesType: "", //糖尿病类型 0:全部 1:1型 2:2型 3:妊娠 4:其他
        doctorId: "", //医生id
        startTime: "", //开始日期
        endTime: "", //结束日期
        hospitalNum: "", //住院号
        patientName: "", //患者名称
        phone: "", //手机号
      },
      departmentlist: [],
      doctorlist: [],
      // 糖尿病类型
      sugarTypelist: [
        {
          label: "无",
          value: "0",
        },
        {
          label: "1型",
          value: "1",
        },
        {
          label: "2型",
          value: "2",
        },
        {
          label: "妊娠",
          value: "3",
        },
        {
          label: "其他",
          value: "4",
        },
      ],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.getAllOutInfo();
    });
    // 获取所有科室
    this.getDepartment();
  },
  methods: {
    // 获取所有出院数据
    getAllOutInfo(value) {
      let time = this.conditionList.time;
      if (time && time.length > 0) {
        this.conditionList.startTime = time[0];
        this.conditionList.endTime = time[1];
      }
      let data = this.conditionList;
      data.current = value ? value.page : 1;
      this.$api
        .getAllOutInfo(data)
        .then((res) => {
          bus.$emit("outInfoList", res);
          this.$emit("setTotal", res.total);
        })
        .catch((err) => {
          bus.$emit("outInfoList", err);
        });
    },
    // 获取科室信息
    getDepartment() {
      let routeid = sessionStorage.getItem("routeID");
      let perId = routeid ? JSON.parse(routeid)["/recoveryPatient"] : "";
      let data = {
        perId,
      };
      this.$api.getDepartment(data).then((res) => {
        this.departmentlist = res.data.data;
      });
    },
    // 科室改变后,获取对应科室医生
    departmentChange(id) {
      // 科室改变先清空医生列表,以及对应医生
      this.doctorlist = [];
      this.conditionList.doctorId = "";
      if (!id) {
        return;
      }
      let doctorlist = this.departmentlist.filter((item) => {
        return item.id == id;
      });

      this.doctorlist = doctorlist[0].bloodSugarDoctorList;
    },
  },
};
</script>

<style lang="scss" scoped>
.leaveHospital-search {
  .el-form {
    position: relative;
    .el-form-item {
      & >>> .el-form-item__content {
        .el-input__inner {
          max-width: 180px;
        }
      }
    }
  }
}
</style>